<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
			label-width="68px">
			<el-form-item label="抢购时间">
				<el-date-picker v-model="daterangeSnappedTime" style="width: 240px" value-format="yyyy-MM-dd HH:mm:ss"
					type="datetimerange" range-separator="-" start-placeholder="开始日期"
					end-placeholder="结束日期"></el-date-picker>
			</el-form-item>
			<el-form-item label="抢购状态" prop="limitStatus">
				<el-select v-model="queryParams.limitStatus" placeholder="请选择抢购状态" clearable>
					<el-option v-for="dict in dict.type.limit_status" :key="dict.value" :label="dict.label"
						:value="dict.value" />
				</el-select>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
				<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>

		<el-row :gutter="10" class="mb8">
			<el-col :span="1.5">
				<el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
					v-hasPermi="['limitCouponMatchesConfig:limitCouponMatchesConfig:add']">新增</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
					v-hasPermi="['limitCouponMatchesConfig:limitCouponMatchesConfig:edit']">修改</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple"
					@click="handleDelete"
					v-hasPermi="['limitCouponMatchesConfig:limitCouponMatchesConfig:remove']">删除</el-button>
			</el-col>
			<!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['limitCouponMatchesConfig:limitCouponMatchesConfig:export']"
        >导出</el-button>
      </el-col> -->
			<right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
		</el-row>

		<el-table v-loading="loading" :data="limitCouponMatchesConfigList" @selection-change="handleSelectionChange">
			<el-table-column type="selection" width="55" align="center" />
			<el-table-column label="ID" align="center" prop="id" />
			<el-table-column label="抢购时间" align="center" prop="snappedTimeVar" width="180">
				<template slot-scope="scope">
					<span>{{ parseTime(scope.row.snappedTimeVar) }}</span>
				</template>
			</el-table-column>
			<el-table-column label="抢购状态" align="center" prop="limitStatus">
				<template slot-scope="scope">
					<dict-tag :options="dict.type.limit_status" :value="scope.row.limitStatus" />
				</template>
			</el-table-column>
			<el-table-column label="活动时长" align="center" prop="limitCouponExpiredTime">
				<template slot-scope="scope">
					{{scope.row.limitCouponExpiredTime}}分钟
				</template>
			</el-table-column>
			<el-table-column label="创建时间" align="center" prop="createTime" width="180">
				<template slot-scope="scope">
					<span>{{ parseTime(scope.row.createTime) }}</span>
				</template>
			</el-table-column>
			<el-table-column label="操作" align="center" class-name="small-padding fixed-width">
				<template slot-scope="scope">
					<el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
						v-hasPermi="['limitCouponMatchesConfig:limitCouponMatchesConfig:edit']">修改</el-button>
					<el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
						v-hasPermi="['limitCouponMatchesConfig:limitCouponMatchesConfig:remove']">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
			@pagination="getList" />

		<!-- 添加或修改限时卡券场次对话框 -->
		<el-drawer :title="title" :visible.sync="open" append-to-body size="70%">
			<div>
				<el-form ref="form" :model="form" :rules="rules" label-width="80px">
					<el-form-item label="抢购时间" prop="snappedTimeVar">
						<el-date-picker clearable v-model="form.snappedTimeVar" type="datetime"
							value-format="yyyy-MM-dd HH:mm:ss" placeholder="请选择抢购时间">
						</el-date-picker>
					</el-form-item>
					<el-form-item label="抢购状态" prop="limitStatus">
						<el-radio-group v-model="form.limitStatus">
							<el-radio v-for="dict in dict.type.limit_status" :key="dict.value"
								:label="parseInt(dict.value)">{{dict.label}}</el-radio>
						</el-radio-group>
					</el-form-item>
					<el-form-item label="活动时长" prop="limitCouponExpiredTime">
						<el-input v-model="form.limitCouponExpiredTime" placeholder="请输入活动时长" class="elInput">
							<template slot="append">分钟</template>
						</el-input>
					</el-form-item>
					<el-form-item label="限时卡券">
						<el-button type="primary" icon="el-icon-plus" @click="limitHandleAdd">新增</el-button>
					</el-form-item>
					<el-table v-loading="loading" :data="limitSpecsList" class="elTable">
						<el-table-column label="ID" align="center" prop="id" v-if="form.id" />
						<el-table-column label="满多少" align="center" prop="couponFull" />
						<el-table-column label="减多少" align="center" prop="couponSubtract" />
						<el-table-column label="优惠券数量" align="center" prop="couponTotal" />
						<el-table-column label="上架状态" align="center" prop="couponStatus">
							<template slot-scope="scope">
								<dict-tag :options="dict.type.is_shelves" :value="scope.row.couponStatus" />
							</template>
						</el-table-column>
						<el-table-column label="用户领取数量" align="center" prop="couponLimitReceiveTotal" />
						<el-table-column label="适用范围" align="center" prop="couponScopeApplication">
							<template slot-scope="scope">
								<el-button type="primary" plain size="mini"
									v-if="scope.row.couponScopeApplication==0">全场通用</el-button>
								<el-button type="success" plain size="mini"
									v-else>{{scope.row.technicianName}}专用</el-button>
							</template>
						</el-table-column>
						<el-table-column label="有效天数" align="center" prop="effectDay" />
						<el-table-column label="创建时间" align="center" prop="createTime" width="180">
							<template slot-scope="scope">
								<span>{{ parseTime(scope.row.createTime) }}</span>
							</template>
						</el-table-column>
						<el-table-column label="操作" align="center" class-name="small-padding fixed-width">
							<template slot-scope="scope">
								<el-button size="mini" type="text" icon="el-icon-edit"
									@click="limitHandleUpdate(scope.row)"
									v-hasPermi="['limitCouponConfig:limitCouponConfig:edit']">修改</el-button>
								<el-button size="mini" type="text" icon="el-icon-delete"
									@click="limitHandleDelete(scope.row)"
									v-hasPermi="['limitCouponConfig:limitCouponConfig:remove']">删除</el-button>
							</template>
						</el-table-column>
					</el-table>

				</el-form>
				<div slot="footer" class="dialog-footer">
					<el-button type="primary" @click="submitForm">确 定</el-button>
					<el-button @click="cancel">取 消</el-button>
				</div>
			</div>
		</el-drawer>

		<!-- 添加或修改限时卡券对话框 -->
		<el-dialog :title="limitTitle" :visible.sync="limitOpen" width="500px" append-to-body>
			<el-form ref="form" :model="limitForm" :rules="rules" label-width="80px">
				<el-form-item label="满多少" prop="couponFull">
					<el-input v-model="limitForm.couponFull" placeholder="请输入" />
				</el-form-item>
				<el-form-item label="减多少" prop="couponSubtract">
					<el-input v-model="limitForm.couponSubtract" placeholder="请输入" />
				</el-form-item>
				<el-form-item label="优惠券数量" prop="couponTotal">
					<el-input v-model="limitForm.couponTotal" placeholder="请输入" />
				</el-form-item>
				<el-form-item label="上架状态" prop="couponStatus">
					<el-radio-group v-model="limitForm.couponStatus">
						<el-radio v-for="dict in dict.type.is_shelves" :key="dict.value"
							:label="parseInt(dict.value)">{{dict.label}}</el-radio>
					</el-radio-group>
				</el-form-item>
				<el-form-item label="用户领取数量" prop="couponLimitReceiveTotal">
					<el-input v-model="limitForm.couponLimitReceiveTotal" placeholder="请输入" />
				</el-form-item>
				<el-form-item label="适用范围" prop="couponScopeApplication">
					<el-radio-group v-model="couponStatus">
						<el-radio v-for="dict in isShelves" :key="dict.value"
							:label="parseInt(dict.value)">{{dict.label}}</el-radio>
					</el-radio-group>
				</el-form-item>

				<el-form-item label="适用技师" prop="couponScopeApplication" v-if="couponStatus==1">
					<el-select v-model="limitForm.couponScopeApplication" placeholder="请选择">
						<el-option v-for="item in technicianList" :key="item.id" :label="item.technicianName"
							:value="item.id">
						</el-option>
					</el-select>
				</el-form-item>
				<!-- <el-table-column label="用户领取数量" align="center" prop="couponLimitReceiveTotal" /> -->
				<el-form-item label="有效天数" prop="effectDay">
					<el-input v-model="limitForm.effectDay" placeholder="请输入有效天数" />
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="limitSubmitForm">确 定</el-button>
				<el-button @click="limitCancel">取 消</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
	import {
		listLimitCouponMatchesConfig,
		getLimitCouponMatchesConfig,
		delLimitCouponMatchesConfig,
		addLimitCouponMatchesConfig,
		updateLimitCouponMatchesConfig
	} from "@/api/limitCouponMatchesConfig/limitCouponMatchesConfig";
	import {
		listLimitCouponConfig,
		getLimitCouponConfig,
		delLimitCouponConfig,
		addLimitCouponConfig,
		updateLimitCouponConfig
	} from "@/api/limitCouponConfig/limitCouponConfig";
	import {
		listTechnician
	} from "@/api/technician/technician";
	export default {
		name: "LimitCouponMatchesConfig",
		dicts: ['limit_status', 'is_shelves'],
		data() {
			return {
				couponStatus: null,
				isShelves: [{
					label: '全场通用',
					value: 0
				}, {
					label: '技师专用',
					value: 1
				}],
				// 遮罩层
				loading: true,
				// 选中数组
				ids: [],
				// 非单个禁用
				single: true,
				// 非多个禁用
				multiple: true,
				// 显示搜索条件
				showSearch: true,
				// 总条数
				total: 0,
				// 限时卡券场次表格数据
				limitCouponMatchesConfigList: [],
				limitCouponConfigList: [],
				technicianList: [],
				// 弹出层标题
				title: "",
				limitTitle: '',
				// 是否显示弹出层
				open: false,
				limitOpen: false,
				// 有效期时间时间范围
				daterangeSnappedTime: [],
				// 查询参数
				queryParams: {
					pageNum: 1,
					pageSize: 10,
					snappedTime: null,
					limitStatus: null,
				},
				// 表单参数
				form: {},
				limitForm: {
					couponFull: '',
					couponSubtract: '',
					couponTotal: '',
					couponStatus: '',
					couponLimitReceiveTotal: '',
					couponScopeApplication: '',
					effectDay: '',
				},
				limitSpecsList: [],
				// 表单校验
				rules: {}
			};
		},
		created() {
			this.getList();
		},
		methods: {
			/** 查询限时卡券场次列表 */
			getList() {
				this.loading = true;
				this.queryParams.params = {};
				if (null != this.daterangeSnappedTime && '' != this.daterangeSnappedTime) {
					this.queryParams.params["beginSnappedTime"] = this.daterangeSnappedTime[0];
					this.queryParams.params["endSnappedTime"] = this.daterangeSnappedTime[1];
				}
				listLimitCouponMatchesConfig(this.queryParams).then(response => {
					this.limitCouponMatchesConfigList = response.rows;
					this.total = response.total;
					this.loading = false;
				});
			},
			/** 查询限时卡券列表 */
			limitList() {
				this.loading = true;
				let data = {
					...this.queryParams,
					matchesId: this.form.id
				}
				listLimitCouponConfig(data).then(response => {
					// this.limitCouponConfigList = response.rows;
					this.limitSpecsList = response.rows;
					this.total = response.total;
					this.loading = false;
				});
			},
			/** 查询技师管理列表 */
			techList() {
				listTechnician({
					pageNum: 1,
					pageSize: 1000,
				}).then(response => {
					this.technicianList = response.rows;
				});
			},
			// 取消按钮
			cancel() {
				this.open = false;
				this.reset();
			},
			limitCancel() {
				this.limitOpen = false;
				this.limitReset()
			},
			// 表单重置
			reset() {
				this.form = {
					id: null,
					snappedTime: null,
					limitStatus: null,
					limitCouponExpiredTime: null,
					isDelete: null,
					createTime: null,
					updateTime: null
				};
				this.resetForm("form");
			},
			// 表单重置
			limitReset() {
				this.limitForm = {
					id: null,
					itemId: null,
					matchesId: null,
					couponFull: null,
					couponSubtract: null,
					couponTotal: null,
					couponStatus: null,
					couponLimitReceiveTotal: null,
					couponScopeApplication: null,
					effectDay: null,
					version: null,
					isDelete: null,
					createTime: null,
					updateTime: null
				};
				this.couponStatus = null,
					this.resetForm("limitForm");
			},
			/** 搜索按钮操作 */
			handleQuery() {
				this.queryParams.pageNum = 1;
				this.getList();
			},
			/** 重置按钮操作 */
			resetQuery() {
				this.daterangeSnappedTime = [];
				this.resetForm("queryForm");
				this.handleQuery();
			},
			// 多选框选中数据
			handleSelectionChange(selection) {
				this.ids = selection.map(item => item.id)
				this.single = selection.length !== 1
				this.multiple = !selection.length
			},
			/** 新增按钮操作 */
			handleAdd() {
				this.reset();
				this.limitSpecsList = []
				this.open = true;
				this.title = "添加限时卡券场次";
			},
			/** 新增按钮操作 */
			limitHandleAdd() {
				this.limitReset();
				this.limitOpen = true;
				this.limitTitle = "添加限时卡券";
				this.techList()
			},
			/** 修改按钮操作 */
			handleUpdate(row) {
				this.reset();
				const id = row.id || this.ids
				getLimitCouponMatchesConfig(id).then(response => {
					this.form = response.data;
					this.open = true;
					this.title = "修改限时卡券场次";
					// this.limitForm = this.form.
					this.limitList()
				});
			},
			/** 修改卡券按钮操作 */
			limitHandleUpdate(row) {
				this.limitReset();
				const id = row.id || this.ids
				getLimitCouponConfig(id).then(response => {
					this.limitForm = response.data;
					response.data.couponScopeApplication == 0 ? this.couponStatus = 0 : this.couponStatus = 1
					this.limitOpen = true;
					this.limitTitle = "修改限时卡券";
					this.techList()
				});
			},
			/** 提交按钮 */
			submitForm() {
				this.$refs["form"].validate(valid => {
					if (valid) {
						if (this.form.id != null) {
							updateLimitCouponMatchesConfig(this.form).then(response => {
								this.$modal.msgSuccess("修改成功");
								this.open = false;
								this.getList();
							});
						} else {

							this.form.limitCouponConfigs = this.limitSpecsList
							addLimitCouponMatchesConfig(this.form).then(response => {
								this.$modal.msgSuccess("新增成功");
								this.open = false;
								this.getList();
							});

							this.limitSpecsList = []
						}
					}
				});
			},
			limitSubmitForm() {
				console.log(this.limitForm)
				if (this.title == '添加限时卡券场次') {
					this.$modal.msgSuccess("新增成功");
					this.limitForm.itemId = new Date().getTime()
					this.limitSpecsList.push(this.limitForm);
					this.limitOpen = false;
					this.limitForm.couponScopeApplication = this.couponStatus
				}
				if (this.title == '修改限时卡券场次') {
					this.couponStatus == 0 ? this.limitForm.couponScopeApplication = 0 : this.limitForm
						.couponScopeApplication = this.limitForm.couponScopeApplication
					this.$refs["form"].validate(valid => {
						if (valid) {
							if (this.limitForm.id != null) {
								updateLimitCouponConfig(this.limitForm).then(response => {
									this.$modal.msgSuccess("修改成功");
									this.limitOpen = false;
									this.limitList();
								});
							} else {
								addLimitCouponConfig(this.limitForm).then(response => {
									this.$modal.msgSuccess("新增成功");
									this.limitOpen = false;
									this.limitList();
								});
							}
						}
					});
				}
			},
			/** 删除按钮操作 */
			handleDelete(row) {
				const ids = row.id || this.ids;
				this.$modal.confirm('是否确认删除限时卡券场次编号为"' + ids + '"的数据项？').then(function() {
					return delLimitCouponMatchesConfig(ids);
				}).then(() => {
					this.getList();
					this.$modal.msgSuccess("删除成功");
				}).catch(() => {});
			},
			/** 删除按钮操作 */
			limitHandleDelete(row) {
				// const ids = row.id || this.ids;
				// this.$modal.confirm('是否确认删除限时卡券编号为"' + ids + '"的数据项？').then(function() {
				// 	return delLimitCouponConfig(ids);
				// }).then(() => {
				// 	this.limitList();
				// 	this.$modal.msgSuccess("删除成功");
				// }).catch(() => {});

				this.limitSpecsList.find((item, index) => {
					if (row.itemId == item.itemId) this.limitSpecsList.splice(index, 1)
				})
			},
			/** 导出按钮操作 */
			handleExport() {
				this.download('limitCouponMatchesConfig/limitCouponMatchesConfig/export', {
					...this.queryParams
				}, `limitCouponMatchesConfig_${new Date().getTime()}.xlsx`)
			}
		}
	};
</script>

<style lang="scss" scoped>
	.elDrawer {
		width: 70% !important;
	}

	.elTable {
		width: 95%;
		margin: auto;
	}

	.dialog-footer {
		margin: 20px;
	}
</style>